﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using WhutDoomCheck.Server.Configs;
using WhutDoomCheck.Server.Entities;
using WhutDoomCheck.Server.Services;
using WhutDoomCheck.Shared.HttpModels;

namespace WhutDoomCheck.Server.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class MetaController : ControllerBase
    {
        public MetaController(MetaDataService metaService, ApplicationDbContext dbContext)
        {
            this.metaService = metaService;
            this.dbContext = dbContext;
        }

        private readonly MetaDataService metaService;
        private readonly ApplicationDbContext dbContext;

        /// <summary>
        /// 获取所有宿舍列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("[Action]")]
        [Authorize(IdentityPolicyNames.Student)]
        public DoomMeta GetDooms() => metaService.GetDooms();

        /// <summary>
        /// 导入学生数据
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpGet("[Action]")]
        public async Task<IActionResult> SeedStudents([FromForm] IFormFile file)
        {
            await this.metaService.SeedStudents(file.OpenReadStream());
            return Ok();
        }

        /// <summary>
        /// 导入宿舍数据
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpGet("[Action]")]
        public async Task<IActionResult> SeedDooms([FromForm] IFormFile file)
        {
            await this.metaService.SeedDooms(file.OpenReadStream());
            return Ok();
        }

        /// <summary>
        /// 追加宿舍数据
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost("[Action]")]
        public async Task<IActionResult> AppendDooms([FromForm] IFormFile file)
        {
            await this.metaService.AppendDooms(file.OpenReadStream());
            return Ok();
        }

        /// <summary>
        /// 导入行政区划数据
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpGet("[Action]")]
        public async Task<IActionResult> SeedRegions([FromForm] IFormFile file)
        {
            await this.metaService.SeedRegions(file.OpenReadStream());
            return Ok();
        }

        /// <summary>
        /// 获取省
        /// </summary>
        /// <returns></returns>
        [HttpGet("[Action]")]
        public async Task<IEnumerable<CNRegion>> GetProvinces()
        {
            return await (from region in dbContext.Regions
                   where region.Level == 1
                   select new CNRegion(region.RegionId, region.Name)).ToListAsync();
        }

        /// <summary>
        /// 获取区域子级
        /// </summary>
        /// <param name="parent"></param>
        /// <returns></returns>
        [HttpGet("[Action]")]
        public async Task<IEnumerable<CNRegion>> GetChildren(long parent)
        {
            return await (from region in dbContext.Regions
                          where region.ParentId == parent
                          select new CNRegion(region.RegionId, region.Name)).ToListAsync();
        }
    }
}
